package com.thinkit.bigdata.core.util.es.elasticsearch;

import com.thinkit.bigdata.web.coremet.SystemStatic;
import org.elasticsearch.client.Client;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.transport.TransportAddress;
import org.elasticsearch.transport.client.PreBuiltTransportClient;

import java.net.InetAddress;
import java.net.UnknownHostException;

/**
 * es 的ClientFactory
 */
public class ClientFactory {

    //根据ip获取es客户端连接
    static Client transportClient() throws UnknownHostException {
        Settings settings = Settings.builder()
                .put("cluster.name", SystemStatic.ES_CLUSTER_NAME)
                .put("client.transport.sniff", true)
                //增加线程池个数，暂时设为5
                .put("thread_pool.search.size", 5)
                .build();
//        PreBuiltTransportClient transPort = new PreBuiltTransportClient(settings);
//        String esIps[] = SystemStatic.ES_IP.split(",");
//        for (String esIp : esIps) {//添加集群IP列表
//            TransportAddress transportAddress =  new TransportAddress(
//                    InetAddress.getByName(esIp),
//                    9300);
//            transPort.addTransportAddress(transportAddress);
//        }
//        return transPort;
        return new PreBuiltTransportClient(settings)
                .addTransportAddress(new TransportAddress(InetAddress.getByName(SystemStatic.ES_IP), Integer.parseInt(SystemStatic.ES_PORT)));
    }
}